package app1.rapidsquare.com.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.LinkedList;

import app1.rapidsquare.com.classes.User;
import app1.rapidsquare.com.Utility.Utility;
import app1.rapidsquare.com.activity.R;

/**
 * Created by Hikmat on 15.08.2014.
 */
public class ContactAdapter extends BaseAdapter {

    private LinkedList<User> contacts;
    private Activity activity;
    private Picasso picasso;

    public ContactAdapter(Activity activity, LinkedList<User> contactList) {
        this.activity = activity;
        this.contacts = contactList;
        this.picasso = Picasso.with(activity);
    }

    @Override
    public int getCount() {
        return contacts.size();
    }

    public LinkedList<User> getContacts() {
        return this.contacts;
    }

    public boolean contains(User contact) {
        for (User item : contacts) {
            if (item.Code.equals(contact.Code))
                return true;
        }
        return false;
    }

    @Override
    public Object getItem(int position) {
        if (contacts != null && contacts.size() > position)
            return contacts.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ContactViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater layoutInflater = activity.getLayoutInflater();

            convertView = layoutInflater.inflate(R.layout.adapter_contact,
                    parent, false);

            holder = new ContactViewHolder();
            holder.Logo = (ImageView) convertView
                    .findViewById(R.id.imageViewLogo);
            holder.FullName = (TextView) convertView
                    .findViewById(R.id.textViewFullName);
            holder.Company = (TextView) convertView
                    .findViewById(R.id.textViewCompany);
            convertView.setTag(holder);

        } else {
            holder = (ContactViewHolder) convertView.getTag();
        }

        User contact = (User) getItem(position);
        if (contact != null) {
            holder.FullName.setText(contact.FirstName + " " + contact.LastName);
            String titleAndCompany = "";

            if (!Utility.isNullOrEmpty(contact.Title) && !Utility.isNullOrEmpty(contact.Company)) {
                titleAndCompany = contact.Title + " at " + contact.Company;
            }

            if (!Utility.isNullOrEmpty(contact.Title) && Utility.isNullOrEmpty(contact.Company)) {
                titleAndCompany = contact.Title;
            }

            if (Utility.isNullOrEmpty(contact.Title) && !Utility.isNullOrEmpty(contact.Company)) {
                titleAndCompany = contact.Company;
            }

            holder.Company.setText(titleAndCompany);

            if (holder.Company.getText().length() < 1)
                holder.Company.setVisibility(View.GONE);
            else
                holder.Company.setVisibility(View.VISIBLE);

            if (!Utility.isNullOrEmpty(contact.Code)) {
                LoadPhoto(contact.Code, holder.Logo);
            } else
                holder.Logo.setImageResource(android.R.color.transparent);
        }

        return convertView;
    }

    private class ContactViewHolder {

        public ImageView Logo;
        public TextView FullName, Company;
    }

    private void LoadPhoto(String code, ImageView imageView) {
        picasso.load(Utility.IMAGE_URL + "?Code=" + code)
                .placeholder(R.drawable.icon_temp)
                .error(R.drawable.icon_temp)
                .resizeDimen(R.dimen.image_size,
                        R.dimen.image_size).centerInside().into(imageView);

    }
}
